package com.ansion.commonchat.startup.workmanager.bwork.impl;

import androidx.annotation.NonNull;

import com.ansion.commonchat.startup.workmanager.bwork.impl.mode.BWorkSpec;
import com.ansion.commonchat.startup.workmanager.bwork.impl.mode.BWorkSpecDao;
import com.ansion.commonchat.startup.workmanager.bwork.impl.mode.WorkDataCache;

import java.util.List;

/**
 * Created by wanghongjie on 2023/5/5
 * Describe :  用于管理任务的调度器。
 * <p>
 * WorkManager 内部用于管理任务调度器的组件。
 * 它负责初始化和调度不同类型的任务调度器，处理任务调度的限制，并提供方法用于调度任务的执行
 */
public class BSchedulers {

    public static void schedule(
            @NonNull WorkDataCache workDataCache,
            List<BScheduler> schedulers) {
        if (schedulers == null || schedulers.size() == 0) {
            return;
        }
        BWorkSpecDao workSpecDao = workDataCache.workSpecDao();
        List<BWorkSpec> allEligibleWorkSpecs;
        allEligibleWorkSpecs = workSpecDao.getAllEligibleWorkSpecsForScheduling();
        if (allEligibleWorkSpecs != null && allEligibleWorkSpecs.size() > 0) {
            BWorkSpec[] enqueuedWorkSpecsArray = new BWorkSpec[allEligibleWorkSpecs.size()];
            enqueuedWorkSpecsArray = allEligibleWorkSpecs.toArray(enqueuedWorkSpecsArray);
            for (BScheduler scheduler : schedulers) {
                if (!scheduler.hasLimitedSchedulingSlots()) {
                    scheduler.schedule(enqueuedWorkSpecsArray);
                }
            }
        }
    }

    private BSchedulers() {
    }
}